#include "tds.h"

#define VREF 3.3        // ADC参考电压
#define StorageDepth 30 // 采样数量

volatile u16_t TDSRawDirty[StorageDepth];
volatile u16_t TDSRawClear[StorageDepth];
volatile u16_t StorageCount = 0;
volatile float ppm_d, ppm_c;
float averageVoltage = 0;
volatile bool stopFlag = false;
void TDSInit();
void GetTDSRaw(void *pvParameter);
void read_ppm(float *ppm_dirty, float *ppm_clear, float temp_dirty, float temp_clear);
int getMedianNum(volatile u16_t bArray[], int iFilterLen);

#ifdef ENABLE_TDS
void setup()
{
    Serial.begin(115200);
    TDSInit();
}

void loop()
{

    GetTDS(&ppm_d, &ppm_c, 25, 25);
    delay(1000);
}
#endif

void TDSInit()
{
    pinMode(TDS_PIN_DIRTY, INPUT);
    pinMode(TDS_PIN_CLEAR, INPUT);
    xTaskCreatePinnedToCore(GetTDSRaw, "TDS Collector", 2048, NULL, 3, NULL, 1);
}

// 获取原始电压值
void GetTDSRaw(void *pvParameter)
{

    while (1)
    {
        if (!stopFlag)
        {
            TDSRawDirty[StorageCount] = analogRead(TDS_PIN_DIRTY);
            TDSRawClear[StorageCount] = analogRead(TDS_PIN_CLEAR);
            StorageCount++;
            if (StorageCount == StorageDepth)
                StorageCount = 0;
            vTaskDelay(50 / portTICK_PERIOD_MS);
        }
    }
}

void read_ppm(float *ppm_dirty, float *ppm_clear, float temp_dirty, float temp_clear)
{
    float compensationCoefficient, compensationVolatge;
    // 暂停一下ADC
    stopFlag = true;
    // 污水缸TDS
    averageVoltage = getMedianNum(TDSRawDirty, StorageDepth) * (float)VREF / 4095.0;
    compensationCoefficient = 1.0 + 0.02 * (temp_dirty - 25.0);
    compensationVolatge = averageVoltage / compensationCoefficient;
    *ppm_dirty = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5;
    // 净水缸TDS
    averageVoltage = getMedianNum(TDSRawClear, StorageDepth) * (float)VREF / 4095.0;
    compensationCoefficient = 1.0 + 0.02 * (temp_clear - 25.0);
    compensationVolatge = averageVoltage / compensationCoefficient;
    *ppm_clear = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5;
    stopFlag = false;

    // Serial.print("Dirty TDS Value:");
    // Serial.print(*ppm_dirty, 0);
    // Serial.println("ppm");

    // Serial.print("Clear TDS Value:");
    // Serial.print(*ppm_clear, 0);
    // Serial.println("ppm");
}

int getMedianNum(volatile u16_t bArray[], int iFilterLen)
{
    int bTab[iFilterLen];
    for (byte i = 0; i < iFilterLen; i++)
        bTab[i] = bArray[i];
    int i, j, bTemp;
    for (j = 0; j < iFilterLen - 1; j++)
    {
        for (i = 0; i < iFilterLen - j - 1; i++)
        {
            if (bTab[i] > bTab[i + 1])
            {
                bTemp = bTab[i];
                bTab[i] = bTab[i + 1];
                bTab[i + 1] = bTemp;
            }
        }
    }
    if ((iFilterLen & 1) > 0)
        bTemp = bTab[(iFilterLen - 1) / 2];
    else
        bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
    return bTemp;
}
